import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { requestLogger } from './common/middleware/request.middleware';
import { AuthModule } from './sys/admin/auth/auth.module';
import { MySqlOrmModule, MyThrottlerModule } from './util/config/plugin.config';
import { APP_GUARD, APP_INTERCEPTOR, RouterModule } from '@nestjs/core';
import { AuthGuard } from './common/guard/auth.guard';
import { PermsGuard } from './common/guard/perms.guard';
import { ThrottlerModule } from '@nestjs/throttler';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { baseUrl } from './util/constants';
import { UserModule } from './sys/admin/user/user.module';
import { RoleModule } from './sys/admin/role/role.module';
import { MenuModule } from './sys/admin/menu/menu.module';
import { TransformInterceptor } from './common/interceptor/transform.interceptor';
import { DictModule } from './sys/admin/dict/dict.module';
import { DictSonModule } from './sys/admin/dict-son/dict-son.module';
import { RolesGuard } from './common/guard/roles.guard';
import { RuleInfoModule } from './sys/admin/rule-info/rule-info.module';
import { BookmarkModule } from './sys/my/bookmark/bookmark.module';
import { WeaponModule } from './sys/game/weapon/weapon.module';
import { UserWeaponModule } from './sys/game/user-weapon/user-weapon.module';
import { WeaponUpModule } from './sys/game/weapon-up/weapon-up.module';
import { ShopModule } from './sys/game/shop/shop.module';
import { RechargeModule } from './sys/game/recharge/recharge.module';
import { RecordModule } from './sys/game/record/record.module';
import { MoneyModule } from './sys/game/money/money.module';
import { UserItemModule } from './sys/game/user-item/user-item.module';
import { SignModule } from './sys/game/sign/sign.module';
import { BaseModule } from './sys/common/base/base.module';
import { FileModule } from './sys/common/file/file.module';

@Module({
  imports: [
    MySqlOrmModule,
    // MyThrottlerModule,
    ServeStaticModule.forRoot({
      // http://localhost:3000/api/public/images/avatar/avatar_104.jpg
      rootPath: join(__dirname, '..', 'public'),
      serveRoot: join(baseUrl, 'public'),
    }),
    AuthModule,
    UserModule,
    RoleModule,
    MenuModule,
    DictModule,
    DictSonModule,
    RuleInfoModule,
    BookmarkModule,

    WeaponModule,
    UserWeaponModule,
    WeaponUpModule,
    ShopModule,
    RechargeModule,
    RecordModule,
    MoneyModule,
    UserItemModule,
    SignModule,

    BaseModule,
    FileModule,
    RouterModule.register([
      {
        path: 'sys',
        children: [
          {
            path: 'admin',
            children: [
              { path: 'auth', module: AuthModule },
              { path: 'user', module: UserModule },
              { path: 'role', module: RoleModule },
              { path: 'menu', module: MenuModule },
              { path: 'dict', module: DictModule },
              { path: 'dict-son', module: DictSonModule },
              { path: 'rule-info', module: RuleInfoModule },
            ],
          },
          {
            path: 'my',
            children: [{ path: 'bookmark', module: BookmarkModule }],
          },
          {
            path: 'common',
            children: [
              { path: 'base', module: BaseModule },
              { path: 'file', module: FileModule },
            ],
          },
          {
            path: 'dnf',
            children: [
              { path: 'weapon', module: WeaponModule },
              { path: 'user-weapon', module: UserWeaponModule },
              { path: 'weapon-up', module: WeaponUpModule },
              { path: 'shop', module: ShopModule },
              { path: 'recharge', module: RechargeModule },
              { path: 'record', module: RecordModule },
              { path: 'money', module: MoneyModule },
              { path: 'user-item', module: UserItemModule },
              { path: 'sign', module: SignModule },
            ],
          },
        ],
      },
    ]),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard, // jwt
    },
    {
      provide: APP_GUARD,
      useClass: RolesGuard, // 角色权限
    },
    {
      provide: APP_GUARD,
      useClass: PermsGuard, // 菜单权限
    },
    {
      provide: APP_GUARD,
      useClass: ThrottlerModule,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor, // 转换返回页面的数据
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 还有.exclude用法
    consumer.apply(requestLogger).forRoutes('*');
    // .forRoutes('cats');
    // 也支持通配符
    // forRoutes({ path: 'ab*cd', method: RequestMethod.ALL });
    // .forRoutes({ path: 'cats', method: RequestMethod.GET });
    // .forRoutes(CatsController);
  }
}
